const db = require('../../../db/index');
const myDate = require('../../tool/myDate');
const arrMethod = require('../../tool/arrMethod');
const addCompany = require('./03addCompany');
const mysql = require('mysql');

// 将当天的考勤信息存入到部门考勤表中
exports.addDepartment = (toDay) => {
	// 查出当天所有考勤的用户
	// let toDay = myDate.toDay();
	const sql = 'select * from attendance where day_date=?'
	db.query(sql, toDay, (err, results) => {
		// 保存需要用到的数据arr ,用户id和上班状态
		const arr = results.map((item) => {
			return Object.assign(
				{},
				{ user_id: item.user_id },
				{ office_status: item.office_status }
			)
		})
		// 将 results 中的 用户id 提取出来组成新的数组
		const arrUser_ids = results.map(x => {return x.user_id});
		// 对用户id新数组进行处理
		let User_ids = '';
		for(let i = 0; i< arrUser_ids.length; i++){
			User_ids += "'" + arrUser_ids[i] + "'" + ","
		};
		User_ids = User_ids.substr(0, User_ids.length - 1)
		// 查出各员工对应在的部门
		const sql = "select * from user where id in(" + User_ids + ")";
		db.query(sql, (err, results) => {
			// 给arr添加对应员工的部门id属性 []
			const arrId_department1 = results.map(x => { return x.id_department1 });
			for(let i = 0; i < arr.length; i++ ) {
				Object.defineProperty(arr[i], 'department_id', {
					value: arrId_department1[i],
					writable:true,
					enumerable:true,
					configurable:true
				})
			}
			// 单独提取岗位id作为一个数组
			const oldDepartmentArr = results.map((item) => {
				return Object.assign(
					{},
					{ department_id: item.id_department1 }
				)
			})
			// 筛选出部门里重复的部门,并组成新数组departmentArr
			let departmentArr = [];
			departmentArr = arrMethod.deleteRepetition(oldDepartmentArr, departmentArr);
			// 计算上班和调休的人数,添加到新数组departmentArr中
			let office_number = 0;
			let rest_number = 0;
			for(let i = 0; i < departmentArr.length; i++ ) {
				for(let j = 0; j < arr.length ; j++){
					if (arr[j].office_status === 0 && arr[j].department_id === departmentArr[i].department_id){
						office_number++;
					};
					if (arr[j].office_status === 1 && arr[j].department_id === departmentArr[i].department_id){
						rest_number++;
					}
				};
				Object.defineProperty(departmentArr[i], 'office_number',{
						value: office_number,
						writable:true,
						enumerable:true,
						configurable:true
				});
				Object.defineProperty(departmentArr[i], 'rest_number',{
						value: rest_number,
						writable:true,
						enumerable:true,
						configurable:true
				});
				Object.defineProperty(departmentArr[i], 'day_date',{
						value: toDay,
						writable:true,
						enumerable:true,
						configurable:true
				});
				Object.defineProperty(departmentArr[i], 'total_number',{
						value: office_number + rest_number,
						writable: true,
						enumerable: true,
						configurable: true
				})
				office_number = 0;
				rest_number = 0;
			}
			// 查询当天的部门考勤表(去除重复的数据)
			const sql = 'select * from department_attendance where day_date=?';
			db.query(sql, toDay, (err, results) => {
				if (results.length === 0){
					// 将当天数据存入部门考勤表
					departmentArr = arrMethod.mysqlArr(departmentArr)
					const sql = "insert into department_attendance(department_id, office_number, rest_number, day_date, total_number) values ?";
					db.query(sql, [departmentArr], (err, results) => {
						console.log('更新部门考勤表', toDay);
						// 执行添加公司考勤表
						addCompany.addCompany(toDay);
					});
				} else {
					let updateDepartmentArr = [];
					let addDepartmentArr = [];
					// 去重并分出两个数组
					for( let i = 0 ; i < departmentArr.length ; i++ ){
						for(let j = 0 ; j < results.length ; j++){
							if (departmentArr[i].department_id == results[j].department_id){
								updateDepartmentArr[i] = departmentArr[i]
							}
						}
					}
					addDepartmentArr = arrMethod.getDiffer(departmentArr, updateDepartmentArr)
					// 更新
					let values = [];
					updateDepartmentArr.forEach(function(n, i) { values.push([n, n.department_id, n.day_date]) })
					const model_sql ='update department_attendance set ? where department_id=? and day_date=?';
					let sqls = '';
					// 拼接sql语句
					values.forEach((item, index) => {
						sqls += mysql.format(model_sql, item) + ';'
					})
					db.query(sqls, (err, results) => {
						// 添加
						if(addDepartmentArr.length === 0){
							console.log('更新部门考勤表', toDay);
							// 执行添加公司考勤表
							addCompany.addCompany(toDay);
						} else {
							addDepartmentArr = arrMethod.mysqlArr(addDepartmentArr)
							const sql = "insert into department_attendance(department_id, office_number, rest_number, day_date, total_number) values ?";
							db.query(sql, [addDepartmentArr], (err, results) => {
								console.log('更新部门考勤表', toDay);
								// 执行添加公司考勤表
								addCompany.addCompany(toDay);
							});
						}
					})
				}
			})
		});
	});
}